package org.yankun.exam4j.web.controller.dashboard;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.yankun.exam4j.common.util.TimeUtils;
import org.yankun.exam4j.core.exam.service.ExamService;
import org.yankun.exam4j.core.paper.service.PaperService;
import org.yankun.exam4j.entity.Exam;
import org.yankun.exam4j.entity.Paper;
import org.yankun.exam4j.entity.User;
import org.yankun.exam4j.security.user.service.UserService;
import org.yankun.exam4j.session.SessionManager;
import org.yankun.exam4j.session.UserSessionData;


@Controller("PaperManageController")
@RequestMapping("/dashboard/paper")
public class PaperManageController {
	
	@Resource
	private ExamService examService;
	
	@Resource
	private PaperService paperService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("index")
	public ModelAndView toIndex(HttpServletResponse response,HttpServletRequest request,String email,String password){
		return new ModelAndView("/dashboard/paper/index");
	}
	
	
	@RequestMapping(value="toAddPaper/{examId}")
	public ModelAndView toAddPaperByExamId(HttpServletResponse response,HttpServletRequest request,@PathVariable("examId") Integer examId){
		Map<String, Object> map=new HashMap<String, Object>();
		
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		List<Exam> examList = examService.getExamsByUserId(usd.getUserId());
		
		List<String> hourList = TimeUtils.getTimeString(24);
		List<String> minList = TimeUtils.getTimeString(60);
		
		map.put("examId", examId);
		map.put("examList", examList);
		map.put("hourList", hourList);
		map.put("minList", minList);
		
		return new ModelAndView("/dashboard/paper/add",map);
	}
	
	@RequestMapping("toAddPaper")
	public ModelAndView toAddPaper(HttpServletResponse response,HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		List<Exam> examList = examService.getExamsByUserId(usd.getUserId());
		
		List<String> hourList = TimeUtils.getTimeString(24);
		List<String> minList = TimeUtils.getTimeString(60);
		
		map.put("examList", examList);
		map.put("hourList", hourList);
		map.put("minList", minList);
		
		return new ModelAndView("/dashboard/paper/add",map);
	}
	
	@RequestMapping("doAddPaper")
	public String doAddPaper(HttpServletResponse response,HttpServletRequest request,Paper paper,String beginFullTime,String endFullTime,Integer examId) throws ParseException{
		HttpSession session = request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		
		
		User user = userService.getUserById(usd.getUserId());
		Exam exam = examService.getExamById(examId);
		
		Date beginTime = TimeUtils.StringToDateTime(beginFullTime);
		Date endTime = TimeUtils.StringToDateTime(endFullTime);

		paper.setCreateUserId(user.getUid());
		paper.setUpdateUserId(user.getUid());
		paper.setBeginTime(beginTime);
		paper.setEndTime(endTime);
		paper.setExam(exam);
		paper.setCreateTime(new Date());
		paper.setUpdateTime(new Date());
		
		paperService.addPaper(paper);
		
		return "redirect:/dashboard/question/toAddQuestion/"+paper.getUid()+".do";
	}
	
}